package com.kzd.gtgame.main.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.entity.Game
import com.kzd.gtgame.entity.TransferRole
import com.kzd.gtgame.main.game.adapter.TransferGameAdapter
import com.kzd.gtgame.main.game.adapter.TransferRoleAdapter
import razerdp.basepopup.BasePopupWindow


class GameTransferPopup(
    context: Context,
    val onGameList: (view: View, game: Game) -> Unit,
    val onRoleList: (view: View, role: TransferRole) -> Unit
) : BasePopupWindow(context) {

    init {
        popupGravity = Gravity.BOTTOM
        setOverlayNavigationBar(false)
    }

    private val mTransferGameAdapter = TransferGameAdapter()

    private val mTransferRoleAdapter = TransferRoleAdapter()

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_game_bargain)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.findViewById<ImageView>(R.id.ivGameBargainClose).setOnClickAntiShakeListener {
            dismiss()
        }
    }

    override fun onCreateShowAnimation(width: Int, height: Int): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f,
            Animation.RELATIVE_TO_PARENT, 0f
        ).apply { duration = 300L }
    }

    override fun onCreateDismissAnimation(): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f
        ).apply { duration = 300L }
    }

    fun fillDataGame(gameFeedback: MutableList<Game>): GameTransferPopup {
        contentView.findViewById<TextView>(R.id.tvGameBargainTitle)
            .text = "选择游戏"
        contentView.findViewById<RecyclerView>(R.id.rvGameBargainBody)
            .adapter = mTransferGameAdapter.apply {
            setNewInstance(gameFeedback)
        }
        mTransferGameAdapter.setOnItemClickListener { _, view, position ->
            val itemData = mTransferGameAdapter.getItem(position)
            onGameList(view, itemData)
        }
        return this
    }

    fun fillDataRole(transferRole: MutableList<TransferRole>): GameTransferPopup {
        contentView.findViewById<TextView>(R.id.tvGameBargainTitle)
            .text = "选择角色"
        contentView.findViewById<RecyclerView>(R.id.rvGameBargainBody)
            .adapter = mTransferRoleAdapter.apply {
            setNewInstance(transferRole)
        }
        mTransferRoleAdapter.setOnItemClickListener { _, view, position ->
            val itemData = mTransferRoleAdapter.getItem(position)
            onRoleList(view, itemData)
        }
        return this
    }

}